import Cookie from './cookie.js'
import Api from  './api.js'
import WApi from  '../wj/api.js'
import Base_api from './Base_api.js'

new Vue({
    el:'#detail',
    data(){
        return{
            ManaShow:[],                             //报名人
            uinfos:[],          //当前用户信息
            showdate:[],        //显示页面
            activity:[],        //当前活动
            user:[],            //活动发布人
            carezh:[],          //关注的账号
            fanszh:[],          //账号的粉丝

            day:'',
            hour:'',
            min:'',
            second:'',

            leng:0,
            falg:0,
            guanzhu:0,

            recordcoutn:0,                  //收藏次数
            Records:[],                     //收藏总表
            isRecord:false,                 //是否收藏了
            upRecord:0,                     //更新方式  0,1
            nowRecord:[],                   //当前用户收藏此饭局信息
        }
    },
    methods:{
        async record() {
            if(this.isRecord == false){
                // alert('第一次收藏')
                let ok = await Base_api.addHoutaiRecord(this.activity.id, this.uinfos.id, 1)
                if(ok>0){
                    this.isRecord = true
                    this.recordcoutn = this.recordcoutn +1
                }
            }else {
                if(this.nowRecord[0].tag == 0){
                    // alert('收藏')
                    let aa = Base_api.updateHoutaiRecord(this.activity.id, this.uinfos.id, 1,this.nowRecord[0].id)
                    if(aa>0){
                        this.$message({
                            type: 'success',
                            message: '收藏成功'
                        });
                        this.nowRecord[0].tag = 1
                        this.upRecord = this.nowRecord[0].tag
                        this.recordcoutn = this.recordcoutn +1
                    }
                }else {
                    // alert('取消收藏')
                    let aa = Base_api.updateHoutaiRecord(this.activity.id, this.uinfos.id, 0,this.nowRecord[0].id)
                    if(aa>0){
                        this.$message({
                            type: 'success',
                            message: '取消收藏成功'
                        });
                        this.nowRecord[0].tag = 0
                        this.upRecord = this.nowRecord[0].tag
                        this.recordcoutn = this.recordcoutn -1
                    }
                }
            }

        },
        async care() {         //关注
            //调用关注表的内容
            let careinfo = await WApi.careInfo()
            let caremsg = careinfo.data
            //获取
            for (let i = 0; i < caremsg.length; i++) {
                    //关注的账号
                    if (caremsg[i].user_zh == this.uinfos.id && caremsg[i].care_zh == this.user.id) {
                        this.guanzhu = 1
                    }
            }
            // alert(this.guanzhu)

            let suess = await Base_api.addCare(this.user.nc,this.uinfos.nc)
            if(suess){
                this.$message({
                    type: 'success',
                    message: '关注成功'
                });
                // alert('关注成功')
                this.guanzhu = 1
            }else {
                this.$message({
                    type: 'error',
                    message: '关注失败'
                });
            }


        },
        async info () {
            let zh = sessionStorage.getItem("zh")

            let allinfo = await WApi.login()

            for (let i = 0; i < allinfo.length; i++) {
                if (allinfo[i].zh == zh) {
                    this.uinfos = allinfo[i]
                    break
                }
            }
            this.showdate = JSON.parse(sessionStorage.getItem('item'))
            this.activity = this.showdate.activity
            this.user = this.showdate.user
            // alert('1'+JSON.stringify(this.showdate))
            let abc = document.getElementById("ui")
            abc.style.backgroundImage = 'url('+this.showdate.activity.img+')'

            let obj
            for (let i = 0; i < allinfo.length; i++) {
                if (allinfo[i].nc == this.user.nc) {
                    obj = allinfo[i]
                    break
                }
            }
            //调用关注表的内容
            let careinfo = await WApi.careInfo()
            let caremsg = careinfo.data
            //获取
            for (let i=0;i<caremsg.length;i++){
                for (let j=0;j<allinfo.length;j++){
                    //关注的账号
                    if(caremsg[i].user_zh==zh){
                        if(caremsg[i].care_zh==allinfo[j].zh){
                            this.carezh.push(allinfo[j])
                        }
                    }
                    //粉丝的账号
                    if(caremsg[i].care_zh==zh){
                        if(caremsg[i].user_zh==allinfo[j].zh){
                            this.fanszh.push(allinfo[j])
                        }
                    }
                }
            }

            for(let i=0;i<this.carezh.length;i++){
                if(this.carezh[i].zh == obj.zh){
                    this.guanzhu = 1
                }
            }

            //收藏
            this.Records = Base_api.SelectAllRecord()
            // alert("12321"+JSON.stringify(this.Records))
            for(let i=0;i<this.Records.length;i++){
                if(this.Records[i].activity_id == this.activity.id && this.Records[i].tag == 1){
                    this.recordcoutn = this.recordcoutn+1
                }
                if(this.Records[i].activity_id == this.activity.id && this.Records[i].user_id == this.uinfos.id){
                    this.isRecord = true
                    this.nowRecord[0] = this.Records[i]
                    this.upRecord = this.nowRecord[0].tag
                    // alert(JSON.stringify(this.nowRecord))
                }
            }

            this.gtime()
            await setInterval(this.gtime,1000)
        },
        checkTime(i){ //将0-9的数字前面加上0，例1变为01
            if(i<10) {
                i = "0" + i;
            }
            return i;
        },

        gtime(){            //倒计时
             let start_time = new Date()
             let end_tiem = this.activity.end_time
             let a =  Date.parse(new Date(start_time))
             let b =  Date.parse(new Date(end_tiem))
             let c =  b-a
             if(c>0){
                 var days =  Math.floor(c / (24 * 3600 * 1000));
                 var leave1 = c % (24 * 3600 * 1000);
                 var hours = Math.floor(leave1 / (3600 * 1000));
                 // 计算小时数后剩余的毫秒数
                 var leave2 = leave1 % (3600 * 1000);
                 // 计算相差分钟数
                 var minutes = Math.floor(leave2 / (60 * 1000));
                 //
                 var leave3 = leave2 % (60 * 1000)
                 var second = Math.floor(leave3 / (1000))
                 var time = days + "天" + hours + "时" + minutes + "分" +second  + "秒";
                 console.log(time)
                 this.day=days
                 this.hour=this.checkTime(hours)
                 this.min=this.checkTime(minutes)
                 this.second=this.checkTime(second)
             }else {
                 this.day=0
                 this.hour=0
                 this.min=0
                 this.second=0
                 this.falg=1
             }

        },
        apply(){
            Cookie.setCookie("bm",this.activity.id,'d1')
            Cookie.setCookie("maxpeople",this.activity.max_people,'d1')
            window.location.href='./Sign_up.html'
        }

    },
    async mounted() {
        if (sessionStorage.getItem("zh") == null) {
            this.$alert('请先登录', '系统提示', {
                confirmButtonText: '确定',
                callback: action => {
                    this.$message({
                        type: 'warning',
                        message: '即将跳转到登录界面...'
                    });
                    location.href = './login.html'
                }
            });
        } else {
            await this.info()

            let item = JSON.parse(sessionStorage.getItem("item"))

            this.ManaShow = Base_api.getManaShow(item.user.id,item.activity.id)

            if(this.ManaShow.length ==0)
                this.leng = 0
            else this.leng = this.ManaShow.length
            // this.ManaShow = Base_api.getManaShow(this.uinfos.id,this.aid)
        }
    }
})